/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aic.faults;

import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.Detail;
import javax.xml.soap.DetailEntry;
import javax.xml.namespace.QName;
import java.util.Locale;
import javax.xml.soap.SOAPException;

public class MessageTooLongFaultFactory {

    //pFaultName - URI of ServiceName e.g.: (SMSService/notification_service)
    public static MessageTooLongFault createSOAPMessageTooLongFault(String pFaultActor, int allowedMessageSize, int yourMessageSize, String yourMessage) { //, Customer cus) {

        SOAPFault newFault = null;


        try {
            MessageFactory messageFactory = null;
            messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
            SOAPMessage message = messageFactory.createMessage();
            SOAPBody body = message.getSOAPBody();
            SOAPFault fault = body.addFault();

            QName faultName = new QName(
                    SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE,
                    "Sender");
            fault.setFaultCode(faultName);

            fault.addFaultReasonText("Message is too long", Locale.US);
            fault.setFaultRole(pFaultActor);


            Detail detail = fault.addDetail();

            QName entryName = new QName("http://bittschen.net/aic", "allowed_message_size");
            DetailEntry entry = detail.addDetailEntry(entryName);
            entry.addTextNode(String.valueOf(allowedMessageSize));

            QName entryName2 = new QName("http://bittschen.net/aic", "your_message_size");
            DetailEntry entry2 = detail.addDetailEntry(entryName2);
            entry2.addTextNode(String.valueOf(yourMessageSize));

            QName entryName3 = new QName("http://bittschen.net/aic", "your_message");
            DetailEntry entry3 = detail.addDetailEntry(entryName3);
            entry3.addTextNode(yourMessage);
/**
            QName entryName4 = new QName("http://bittschen.net/aic", "customer");
            DetailEntry entry4 = detail.addDetailEntry(entryName4);

            JAXBContext context = JAXBContext.newInstance(Customer.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(cus, entry4);
**/

            message.saveChanges();


            newFault = body.getFault();/**
        } catch (JAXBException ex) {
            Logger.getLogger(SOAPMessageTooLongFaultFactory.class.getName()).log(Level.SEVERE, null, ex);
   **/     } catch (SOAPException e) {
            System.out.println("Meine Exception" + e);
        }
        MessageTooLongFault mEx = new MessageTooLongFault(newFault);
        return mEx;

    }
}
